const template = require('@babel/template').default
module.exports = function ({ types: t }) {
    function getArg(data) {
       return data.map(item=>{
           if(item.type==='StringLiteral'){
            return t.stringLiteral(item.value)
           }else if(item.type==='CallExpression'){
                const fnNode = t.callExpression(t.identifier(item.callee.name), item.arguments)
                return fnNode
           }
       });
    }

    function getAllUrlParams (str) {
        const q = {};
        str.replace(/([^?&=]+)=([^&]+)/g, (_, k, v) => (q[k] = decodeURI(v)));
        return q;
      }

    return {
        visitor: {
            Identifier(path, state) {
                // const parentIsIf = t.isIfStatement(path.parentPath);
                // const isDebug = path.node.name === "DEBUG";
                // if (parentIsIf && isDebug) {
                //     //把Identifier转换成string
                //     const stringNode = t.stringLiteral("DEBUG")
                //     path.replaceWith(stringNode)
                // }

         
            },
            StringLiteral(path, state) {
                const parentIsIf = t.isIfStatement(path.parentPath);
                const isDebug = path.node.value === "DEBUG";
                if (parentIsIf && isDebug && state.opts.isRemove === "true") {
                    path.parentPath.remove()
                }
            },
            AssignmentExpression(path) {
                // 更改url
                console.log('uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu')
                console.log(path)
                let params=null;
                const right=path.node.right;
                console.log(888888888888888888888888)
                console.log(right.type)
                if(right.type==='Identifier'){
                    params=t.Identifier(right.name);
                    // if (path.scope.hasBinding("n")) {
                    //     // ...
                    //   }
                    
                }else if(right.type==='StringLiteral'){
                    params=t.stringLiteral(right.value);
                    console.log('111111111111111111111111')
                    console.log(getAllUrlParams(right.value))
                }else if(right.type==='MemberExpression'){
                   console.log('aaaaaaaaaaaaaa')
                    
                
                }else if(right.type==='BinaryExpression'||right.type==='TemplateLiteral'||right.type==='ConditionalExpression'){
                    params=right
                }



                const urlObj = t.objectProperty(t.Identifier("url"), params);
                const fnNode = t.callExpression(t.identifier('jumpToPage'), [t.objectExpression([urlObj])])
                path.replaceWith(fnNode)
            },
            CallExpression(path) {
                // 更改setItem
                
                if (path.node.callee?.object?.name === 'localStorage'&&path.node.callee?.property.name==='setItem') {
                    path.replaceWith(
                        t.callExpression(
                            t.memberExpression(t.identifier('ytLocalStorage'), t.identifier('setItem')),
                            getArg(path.node.arguments)
                        )
                    );
                }

                // 更改getItem
                if (path.node.callee?.object?.name === 'localStorage'&&path.node.callee?.property.name==='getItem') {
                    //将ArrowFunctionExpression  转化为  FunctionExpression ，传入不要的参数
                    const body=t.blockStatement([]);
                    let functionExpression = t.FunctionExpression(null,[],body,true);
                    path.replaceWith(functionExpression);
                }
            },
            AwaitExpression(path) {
                // 首先保证 await 语句没有被 try/catch 包裹
                if (path.findParent(path => t.isTryStatement(path.node))) return;
                const parent = path.parent;
                let replacePath = null;
                if (t.isVariableDeclarator(parent) || t.isAssignmentExpression(parent)) {
                  // 赋值和声明的方式结构类似，都是在 AwaitExpression 中 path 的 parentPath.parentPath 上的节点就是 blockStatement 所需要的的参数，可以直接这么替换
                  // console.log("AwaitExpression -> path.parentPath", path.parentPath)
                  replacePath = path.parentPath.parentPath;
                } else {
                  // 如果只是表达式的话，path.parentPath.node 就是 blockStatement 参数
                  replacePath = path.parentPath;
                }
                // console.log("AwaitExpression -> replacePath", replacePath)
                const tryBlock = t.blockStatement([replacePath.node]);
                // 生成 catch --> new Error(e)
                const paramsE = t.identifier('e');
                const throwStatement = t.throwStatement(t.newExpression(t.identifier('Error'), [paramsE]));
                const catchClause = t.catchClause(paramsE, t.blockStatement([throwStatement]));
                const tryStatement = t.tryStatement(tryBlock, catchClause);
                replacePath.replaceWithMultiple([tryStatement]);
              },
              ArrowFunctionExpression: (path, state) => {
                console.log(path.node)
                // console.log(path.parent.id)
                let node = path.node;
                let id = path.parent.id;
                let params = node.params;
                let body=t.blockStatement([
                    t.returnStatement(node.body)
                ]);
                //将ArrowFunctionExpression  转化为  FunctionExpression ，传入不要的参数
                let functionExpression = t.functionExpression(id,params,body,false,false);
                path.replaceWith(functionExpression);
            }
        }
    }
}




